﻿Public Class DefaultJosClient
    Implements IJosClient

    Public Const PARAM_JSON As String = "360buy_param_json"
    Public Const METHOD As String = "method"
    Public Const ACCESS_TOKEN As String = "access_token"
    Public Const APP_KEY As String = "app_key"
    Public Const SIGN As String = "sign"
    Public Const TIME_STAMP As String = "timestamp"
    Public Const VERSION As String = "v"

    Private serverUrl As String
    Private accessToken As String
    Private appKey As String
    Private appSecret As String

    Private disableParser As Boolean = True
    Private disableTrace As Boolean = True

    Sub New(ByVal serverUrl As String, ByVal accessToken As String, ByVal appKey As String, ByVal appSecret As String)
        Me.accessToken = accessToken
        Me.appKey = appKey
        Me.appSecret = appSecret
        Me.serverUrl = serverUrl




    End Sub

    Public Function Execute(Of T As JosResponse)(ByVal request As IJosRequest(Of T)) As T Implements IJosClient.Execute
        Return Execute(request, Nothing)
    End Function

    Public Function Execute(Of T As JosResponse)(ByVal request As IJosRequest(Of T), ByVal session As String) As T Implements IJosClient.Execute
        Return Execute(request, session, DateTime.Now)
    End Function

    Public Function Execute(Of T As JosResponse)(ByVal request As IJosRequest(Of T), ByVal session As String, ByVal timestamp As Date) As T Implements IJosClient.Execute
        Return DoExecute(request, session, timestamp)
    End Function

    Private Function DoExecute(Of T As JosResponse)(ByVal request As IJosRequest(Of T), ByVal session As String, ByVal timestamp As Date) As T
        '提前检查业务参数
        Try
            request.Validate()
        Catch ex As Exception
            Return createErrorResponse(Of T)("1", ex.Message)
        End Try

        '添加协议级请求参数
        Dim txtParams As New JosDictionary
        txtParams.Add(METHOD, request.GetApiName())
        txtParams.Add(VERSION, "2.0")
        txtParams.Add(APP_KEY, appKey)
        txtParams.Add(TIME_STAMP, timestamp)
        txtParams.Add(SIGN, "sign")
        txtParams.Add(PARAM_JSON, request.GetParameters.JsonSerializer)

        Dim body As String = String.Empty
        If TypeOf request Is IJosUploadRequest(Of T) Then

        Else
            'body = WebUtil.DoPost(serverUrl, txtParams)
        End If

        Dim rsp As T
        If disableParser Then
            rsp = Activator.CreateInstance(Of T)()
            rsp.Body = body
        Else
            Dim tp As IJosParser(Of T) = New JosJsonParser(Of T)
            rsp = tp.Parse(body)
        End If

        rsp.ReqUrl = WebUtil.BuildGetUrl(serverUrl, txtParams)

        Return rsp
    End Function

    Public Function createErrorResponse(Of T As JosResponse)(ByVal errCode As String, ByVal errMsg As String) As T
        Dim rsp As T = Activator.CreateInstance(Of T)()
        rsp.Code = errCode
        rsp.ErrMsg = errMsg
        'rsp.Body
        Dim errObj As New Dictionary(Of String, Object)
        errObj.Add("code", errCode)
        errObj.Add("msg", errMsg)
        rsp.Body = JsonSerializer(errObj)
        Return rsp
    End Function

End Class
